Load the tidyverse package and the ggplot2 package.
library(tidyverse)
## ── Attaching packages ─────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 1.0.0
## ✓ tidyr 1.1.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.2
## Warning: package 'tibble' was built under R version 3.6.2
## Warning: package 'tidyr' was built under R version 3.6.2
## Warning: package 'purrr' was built under R version 3.6.2
## Warning: package 'dplyr' was built under R version 3.6.2
## ── Conflicts ────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
Import the chds6162_data to a dataframe called data.
data <- read_csv("data/chds6162_data.csv")
## Parsed with column specification:
## cols(
## .default = col_double(),
## drace = col_character()
## )
## See spec(...) for full column specifications.
Use select to show just the marital variable.
data %>%
select(marital)
select for multiple variables:
Use select to show marital and ed variables.
data %>%
select(marital,ed)
Use select for a range of columns.
select all the variables from wt to the end.
# option one:
data %>%
select(wt:number)
# option 2:
data %>%
select(wt:last_col())
Drop the race variable.
data %>%
select(-race)
Drop the variables that belonged to the father from drace to dwt
data %>%
select(-(drace:dwt))
art by @allison_horst
Create a new variable with a specific value
Create a new variable called country and fill that variable with “US”.
data %>%
mutate(country = "US")
Create a new variable based on other variables
Create a new variable called gestation_weeks and calculate gestation length in weeks rather than days (try rounding this number to only 2 decimals). Remember that the gestation variable is a measure of the gestation length in days. Then select both variables.
data %>%
mutate(gestation_weeks = round(gestation / 7,2)) %>%
select(gestation, gestation_weeks)
Change an existing variable
Convert the dwt variable to kilos by dividing by 2.205 (it’s in pounds now). Then, use select to show only the dwt variable.
data %>%
mutate(dwt = dwt / 2.205) %>%
select(dwt)